package com.wyth.lottery.activity;

import android.app.Activity;
import android.os.SystemClock;
import android.view.View;
import android.widget.TextView;

import com.wyth.lottery.R;
import com.wyth.lottery.base.BaseActivity;
import com.wyth.lottery.utils.MainThread;
import com.wyth.lottery.utils.Vbar;

/**
 * 我要兑奖
 */
public class PrizeActivity extends BaseActivity {
    private boolean state;
    private Vbar b = new Vbar();
    protected boolean decodestate;


    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    private Activity activity;


    private void startVbar() {

        decodestate = true;

        SystemClock.sleep(10);//暂10毫秒后程序继续执行


        //打开扫码器
        state = b.vbarOpen();
        System.out.println("打开扫码器");

        //确认是否打开扫码器
        if (state) {
            decodestate = true;
        } else {
            decodestate = false;
        }

        mThread.start();
    }

    Thread mThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (decodestate) {
                //开始扫码
                final String str = b.vbarScan();
                System.out.println("开始扫码-----------------------------------");
                System.out.println(str + "--------------------------------++++++++++++++++");
                SystemClock.sleep(500);
                if (str != null) {
                    decodestate = false;
                    b.vbarClose();
                    b.vbarBeep(1);
                }
                MainThread instance = MainThread.getInstance();

                instance.post(new Runnable() {
                    @Override
                    public void run() {
                        if (str != null) {
                            startAct(str);

                        }
                    }
                });
            }
        }
    });



    /**
     * 蜂鸣器控制
     *
     * @param
     */
    private void startAct(String s) {
        b.vbarBeep(1);
        String stateStr = "dj";

        if (stateStr.equals("dj")) {
            //开奖公告
        }


    }

    @Override
    protected int getContentID() {
        return R.layout.activity_prize_noprize;
    }

    @Override
    protected void initView() {
        startVbar();

    }

    @Override
    protected void initDate() {
        TextView textView = (TextView) findViewById(R.id.tv_return);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();


                decodestate = false;
            }
        });
    }

}
